// pages/index/BeautyKilling/kill_details/collection/collection.js
var Helpdata={}
// var userId = wx.getStorageSync('userId')
var openids = wx.getStorageSync('openids')
// var shopid = wx.getStorageSync('shopid')
var TopData = {}
var UserInfodata = {}
var nickname = null
var head = null
var shopid = '06017d7fc1d1816446e5a993352f50f2882f'
function getNowFormatDate() {
  var dates = new Date();
  var year = dates.getFullYear();
  var month = dates.getMonth() + 1;
  var date = dates.getDate();
  var hour = dates.getHours() < 10 ? "0" + dates.getHours() : dates.getHours();
  var minute = dates.getMinutes() < 10 ? "0" + dates.getMinutes() : dates.getMinutes();
  var second = dates.getSeconds() < 10 ? "0" + dates.getSeconds() : dates.getSeconds();
  // this.milliSeconds = date.getMilliseconds();
  var currentTime = year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
  return currentTime;

}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    width_s:'210%',
    input_value:'',
    countDownList: [],
    actEndTimeList: [],
    src_xing:4,
    pop:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var userIdshaer = options.userIdshaer
    var activityid = options.activityid
    var wareid = options.wareid
    wx.setStorageSync('activityid', activityid)
    wx.setStorageSync('wareid', wareid)
    wx.setStorageSync('userIdshaer', userIdshaer)


    var that = this
    var time_ss = getNowFormatDate()
    wx.showLoading({
      title: '加载中',
    })

    // 获取页面数据
    wx.request({
      url: 'https://serv.fullcang.com/shark/sharkActivity/yzsShareInfo',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": userIdshaer,
        "userType": 1,
        "content": {
          "activityId": activityid,
          "wareId": wareid,
          "shareUser": userIdshaer
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        console.log(e)
        wx.hideLoading()

        var data = e.data.content
        var time = e.data.content.countDown

        var userPartList = e.data.content.userPartList
        var userTopThreeList = e.data.content.userTopThreeList

        var width_s = userPartList.length*30+'%'

        that.setData({
          content:data,
          userTopThreeList: userTopThreeList,
          userPartList: userPartList,
          width_s: width_s,
          time:time
        })
        console.log(e)


        if (time != ''){
          let goodsList = [
            { actEndTime: time },
          ]
          let endTimeList = [];
          // 将活动的结束时间参数提成一个单独的数组，方便操作
          goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
          that.setData({ actEndTimeList: endTimeList });
          // 执行倒计时函数
          that.countDown();
        }
        
      }
    })


    // 授权
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://serv.fullcang.com/shark/sharkUser/find/openid',
            data: {
              "type": 1,
              "seq": 1,
              "sysVer": "0.0.1",
              "sendTime": time_ss,
              "userId": "",
              "resId": "0000",
              "privId": "0000",
              "platform": 2,
              "token": "",
              "shopId": shopid,
              "userType": 3,
              "source": 4,
              "content": {
                "appid": "wx6ca4bda138687ed8",
                "secret": "81b33d96dfbe809cff410a03b54d4c00",
                "usercode": res.code
              }
            },
            method: "POST",
            header: { "Content-Type": "application/JSON" },
            success: function (e) {
              openids = e.data.content.openid;
              wx.setStorageSync('openids', openids)
              console.log(openids)
              wx.getUserInfo({
                success: function (e) {
                  nickname = e.userInfo.nickName
                  head = e.userInfo.avatarUrl
                  // UserInfodata.avatarUrl = e.userInfo.avatarUrl,
                  //   UserInfodata.nickName = e.userInfo.nickName,
                  //   wx.setStorageSync('UserInfodata', UserInfodata)
                  
                  wx.request({
                    url:'https://serv.fullcang.com/shark/sharkUser/wxLogin',
                    data: {
                      "type": 1,
                      "seq": 1,
                      "sysVer": "0.0.1",
                      "sendTime": time_ss,
                      "userId": "default",
                      "resId": "0000",
                      "privId": "0000",
                      "platform": 2,
                      "token": "",
                      "shopId": shopid,
                      "userType": 1,
                      "source": 4,
                      "content": {
                        "openid": openids,
                        "username": nickname,
                        "shopId": "06017d7fc1d1816446e5a993352f50f2882f",
                        "image": head
                      }
                    },
                    method: "POST",
                    header: {
                      "Content-Type": "application/JSON"
                    },
                    success: function (res) {
                      console.log(res)
                      var userId = res.data.userId
                      wx.setStorageSync('userId', userId)
                    }
                  })
                }, fail: function (failData) {
                  wx.hideTabBar({

                  })
                  that.setData({
                    pop: true
                  })
                }
              })

            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  // 获取授权按钮
  bindGetUserInfo: function (e) {
    var time_ss = getNowFormatDate()
    var that = this;
    //此处授权得到userInfo
    //接下来写业务代码
    var   openids = wx.getStorageSync('openids')
    nickname = e.detail.userInfo.nickName
    head = e.detail.userInfo.avatarUrl
    console.log(openid)
    wx.request({
      url: 'https://serv.fullcang.com/shark/sharkUser/wxLogin',
      data: {
        "type": 1,
        "seq": 1,
        "sysVer": "0.0.1",
        "sendTime": time_ss,
        "userId": "default",
        "resId": "0000",
        "privId": "0000",
        "platform": 2,
        "token": "",
        "shopId": "",
        "userType": 1,
        "source": 4,
        "content": {
          "openid": openids,
          "username": nickname,
          "shopId": shopid,
          "image": head
        }
      },
      method: "POST",
      header: {
        "Content-Type": "application/JSON"
      },
      success: function (res) {
        console.log(res)
        var userId = res.data.userId
        wx.setStorageSync('userId', userId)
        wx.showTabBar({

        })
        that.setData({
          pop: false
        })
      }
    })
  },

  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '0',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    this.setData({
      times1: countDownArr[0],

    })
    setTimeout(this.countDown, 1000);

  },
  swichNav:function(e){
    Helpdata={}
    var i = e.currentTarget.dataset.current
    var val = e.currentTarget.dataset.value
    Helpdata = val
    wx.setStorageSync('Helpdata', Helpdata)
    var that = this
    that.setData({
      currentTab: i,
    })
  },
  data_value:function(e){
    var val = e.detail.value
    Helpdata = val
    wx.setStorageSync('Helpdata', Helpdata)
  },
  Assistance:function(){
    var that = this
    that.setData({
      Assistance: true,
    })
  },
  guanbi:function(){
    wx.removeStorageSync('Helpdata')
    var that = this
    that.setData({
      Assistance: false,
      input_value:'',
    })
  },
  // 提交助力金额
  Helpdata:function(e){
    // console.log(openid)
    // var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var activityId = wx.getStorageSync('activityid')
    var wareId = wx.getStorageSync('wareid')
    var userIdshaer = wx.getStorageSync('userIdshaer')
    var time_ss = getNowFormatDate()
    var that = this    
    var k = wx.getStorageSync('Helpdata')*100
    console.log(k)
    if (!k){
      wx.showModal({
        title: '提示',
        content: '请填写助力金额',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            wx.removeStorageSync('Helpdata')
            that.setData({
              Assistance: false,
            })
          }
        }
      })
    }else{
      wx.request({
        url: 'https://serv.fullcang.com/shark/gateCash/payByFaceKill',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId,
          "userType": 1,
          "content": {
            "shopId": shopid,
            "openId": openids,
            "appId": "wx6ca4bda138687ed8",
            "balance": k
          }
        },
        method: "POST",
        header: { "Content-Type": "application/JSON" },
        success: function (e) {

          console.log(e)
          var s = e.data.result.code.substring(4)
          var i_ = e.data.result.desc
          if(s == '9999'){
            var i = e.data.content
            wx.requestPayment({
              timeStamp: i.timeStamp,
              nonceStr: i.nonce_str,
              package: 'prepay_id=' + i.prepay_id,
              signType: 'MD5',
              paySign: i.paySign,
              success(res) {

                wx.request({
                  url: 'https://serv.fullcang.com/shark/gateCash/payByWxSuccess',
                  data: {
                    "list": false,
                    "nodeId": "kdslfw",
                    "platform": 2,
                    "privId": "default",
                    "resId": "default",
                    "sendTime": time_ss,
                    "seq": 1,
                    "shopId": shopid,
                    "source": 11,
                    "sysVer": "1.0.0",
                    "token": "kdsjfoewi12ijsldks",
                    "type": 1,
                    "userId": userId,
                    "userType": 1,
                    "content": {
                      "tradeId": i.tradeId
                    },
                    "page": 1,
                    "limit": 15
                  },
                  method: "POST",
                  header: { "Content-Type": "application/JSON" },
                  success: function (e) {
                  console.log(e+'支付成功')
                    wx.removeStorageSync('Helpdata')
                    that.setData({
                      Assistance: false,
                    });
                    wx.request({
                      url: 'https://serv.fullcang.com/shark/sharkActivity/addYzsProcess',
                      data: {
                        "list": false,
                        "nodeId": "kdslfw",
                        "platform": 2,
                        "privId": "default",
                        "resId": "default",
                        "sendTime": time_ss,
                        "seq": 1,
                        "shopId": shopid,
                        "source": 11,
                        "sysVer": "1.0.0",
                        "sysCode": "0902",
                        "token": "",
                        "type": 1,
                        "userId": userId,
                        "userType": 1,
                        "content": {
                          "activityId": activityId,
                          "wareId": wareId,
                          "payPrice": k,
                          "shareUser": userIdshaer
                        }
                      },
                      method: "POST",
                      header: { "Content-Type": "application/JSON" },
                      success: function (e) {

                        var time_ss = getNowFormatDate()


                        // 获取页面数据
                        wx.request({
                          url: 'https://serv.fullcang.com/shark/sharkActivity/yzsShareInfo',
                          data: {
                            "platform": 2,
                            "privId": "0000",
                            "resId": "0000",
                            "sendTime": time_ss,
                            "seq": "232312313",
                            "shopId": shopid,
                            "source": 1,
                            "sysVer": "0.0.1",
                            "type": 1,
                            "userId": userIdshaer,
                            "userType": 1,
                            "content": {
                              "activityId": activityId,
                              "wareId": wareId,
                              "shareUser": userIdshaer
                            }
                          },
                          method: "POST",
                          header: { "Content-Type": "application/JSON" },
                          success: function (e) {
                            console.log(e)
                            var data = e.data.content
                            var time = e.data.content.countDown

                            var userPartList = e.data.content.userPartList
                            var userTopThreeList = e.data.content.userTopThreeList

                            var width_s = userPartList.length * 30 + '%'

                            that.setData({
                              content: data,
                              userTopThreeList: userTopThreeList,
                              userPartList: userPartList,
                              width_s: width_s
                            })
                            console.log(e)



                            let goodsList = [
                              { actEndTime: time },
                            ]
                            let endTimeList = [];
                            // 将活动的结束时间参数提成一个单独的数组，方便操作
                            goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
                            that.setData({ actEndTimeList: endTimeList });
                            // 执行倒计时函数
                            that.countDown();
                          }
                        })
                      }
                    })
                  }
                })
              },
              fail(res) {
                console.log(res)
              }
            })

          }else if(s == '0000'){
            console.log(e + '支付成功')
            wx.removeStorageSync('Helpdata')
            that.setData({
              Assistance: false,
            });
            wx.request({
              url: 'https://serv.fullcang.com/shark/sharkActivity/addYzsProcess',
              data: {
                "list": false,
                "nodeId": "kdslfw",
                "platform": 2,
                "privId": "default",
                "resId": "default",
                "sendTime": time_ss,
                "seq": 1,
                "shopId": shopid,
                "source": 11,
                "sysVer": "1.0.0",
                "sysCode": "0902",
                "token": "",
                "type": 1,
                "userId": userId,
                "userType": 1,
                "content": {
                  "activityId": activityId,
                  "wareId": wareId,
                  "payPrice": k,
                  "shareUser": userIdshaer
                }
              },
              method: "POST",
              header: { "Content-Type": "application/JSON" },
              success: function (e) {
               
                var time_ss = getNowFormatDate()


                // 获取页面数据
                wx.request({
                  url: 'https://serv.fullcang.com/shark/sharkActivity/yzsShareInfo',
                  data: {
                    "platform": 2,
                    "privId": "0000",
                    "resId": "0000",
                    "sendTime": time_ss,
                    "seq": "232312313",
                    "shopId": shopid,
                    "source": 1,
                    "sysVer": "0.0.1",
                    "type": 1,
                    "userId": userIdshaer,
                    "userType": 1,
                    "content": {
                      "activityId": activityid,
                      "wareId": wareid,
                      "shareUser": userIdshaer
                    }
                  },
                  method: "POST",
                  header: { "Content-Type": "application/JSON" },
                  success: function (e) {
                    console.log(e)
                    var data = e.data.content
                    var time = e.data.content.countDown

                    var userPartList = e.data.content.userPartList
                    var userTopThreeList = e.data.content.userTopThreeList

                    var width_s = userPartList.length * 30 + '%'

                    that.setData({
                      content: data,
                      userTopThreeList: userTopThreeList,
                      userPartList: userPartList,
                      width_s: width_s
                    })
                    console.log(e)



                    let goodsList = [
                      { actEndTime: time },
                    ]
                    let endTimeList = [];
                    // 将活动的结束时间参数提成一个单独的数组，方便操作
                    goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
                    that.setData({ actEndTimeList: endTimeList });
                    // 执行倒计时函数
                    that.countDown();
                  }
                })
              }
            })
          }else{
            wx.showLoading({
              title: i_,
            });
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.removeStorageSync('Helpdata')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})